export class Carts {
    constructor(){
        this.cbList = {};
        // console.log(this.cbList);
    }
    init() {
        //添加购物车;
        let add_cart = $(".add-cart");
        let show_carts = $('.carts');
        let close_cart = $(".close-carts");
        let clear_carts = $(".clear-carts");
        add_cart.on("click", this.addCart.bind(this));
        show_carts.on("click", this.showCartsList.bind(this));
        close_cart.on("click", this.closeCartsList.bind(this));
        clear_carts.on("click", this.clearCartsList.bind(this));
        this.sumCartNum();

        let btn_reduce = $(".btn-reduce");
        let btn_add    = $(".btn-add");
        let goods_list = $(".goods-list");
        goods_list.on("click" ,".btn-reduce",this.reduceGoodsNum.bind(this));
        goods_list.on("click" ,".btn-add",this.addGoodsNum.bind(this));


    }

    addCart(evt) {
        let good_id = $(evt.target).attr("good_id");
        // console.log(good_id);
        this.savaCart(good_id);
        this.sumCartNum();
        this.fire("render_list");
    }


    //存储id和count到localstorage;
    savaCart(id) {
        let s = localStorage.getItem("carts");
        //carts不存在;
        if (s === null) {
            let a = [{
                id: id,
                count: 1,
            }]
            // 设置carts;
            localStorage.setItem("carts", JSON.stringify(a));
        }
        else {
            let la = JSON.parse(s);
            let has_same_id = false;
            $.each(la, function (index, item) {
                if (item.id === id) {
                    item.count++;
                    has_same_id = true;
                }
            })
            if (!has_same_id) {
                //没有一样的id;
                la.push({
                    id: id,
                    count: 1
                })
            }
            //更新localStorage
            localStorage.setItem("carts", JSON.stringify(la));
            console.log(localStorage.getItem("carts"));
        }
    }

    //渲染购物车里的商品数量
    sumCartNum() {
        let ls = localStorage.getItem("carts");
        let la = JSON.parse(ls === null ? "[]" : ls);
        let sum = 0;
        la.forEach(item => {
            sum += item.count;
        });
        let carts = $(".carts");
        carts.html(sum);

    }

    showCartsList(type) {
        let carts_list = $(".carts-list");
        carts_list.addClass("show-carts");
        this.fire("render_list");
    }
    closeCartsList() {
        let carts_list = $(".carts-list");
        carts_list.removeClass("show-carts");
    }
    clearCartsList() {
        let clears = confirm("是否清空购物车?");
        if (clears) {
            localStorage.removeItem("carts");
            this.sumCartNum();
            this.fire("render_list");
        }
    }

    add(cb,type){
        
        //第一次执行,给cblist对象创建属性,属性值是数组;
        if(!(this.cbList[type]   instanceof Array)){
            this.cbList[type] = [];
        }
        //将传进来的函数地址存入数组;
        this.cbList[type].push(cb);
        // console.log(cb,type);
        // console.log(this.cbList);
    }
    fire(type){
        console.log(this.cbList);
        this.cbList[type].forEach(element => {
            typeof element === "function" ? element() : "";
        });
    }
    reduceGoodsNum(evt){
        // console.log($(evt.target).attr("good_id"));
        let good_id = $(evt.target).attr("good_id");
        let la = JSON.parse(localStorage.getItem("carts"));
        la.forEach((item,index) =>{
            if((item.id === good_id))
            {
                item.count -- ;
                if(item.count == 0)
                {
                    la.splice(index,1);
                }
            }
        })
        //更新carts;
        localStorage.setItem("carts",JSON.stringify(la));

        //重新渲染购物车商品列表;
          this.fire("render_list");
        //重新渲染购物车商品数量;
        this.sumCartNum();
      

    }

    addGoodsNum(evt){
        // console.log($(evt.target).attr("good_id"));
          let good_id = $(evt.target).attr("good_id");
          let la = JSON.parse(localStorage.getItem("carts"));
          la.forEach((item,index) =>{
              if((item.id === good_id))
              {
                  item.count ++ ;
                
              }
          })
          //更新carts;
          localStorage.setItem("carts",JSON.stringify(la));
  
          //重新渲染购物车商品列表;
            this.fire("render_list");
          //重新渲染购物车商品数量;
          this.sumCartNum();
    }
}